package com.supermarket.servlet;

import com.supermarket.entity.User;
import com.supermarket.service.Userservice;
import com.supermarket.service.lmpl.UserserviceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class UserServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String action = req.getParameter ( "action" );
        if (action.equals ( "logout" )){
            this.getlogout( req,resp);
        }
    }

    private void getlogout(HttpServletRequest req, HttpServletResponse resp) {
        try {
            resp.sendRedirect ( "/index.jsp" );
        } catch (IOException e) {
            e.printStackTrace ();
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String action = req.getParameter ( "action" );
        if (action.equals ( "login" )){
            //这里进入登录方式的请求

            this.getlogin(req,resp);
        }


    }

    private void getlogin(HttpServletRequest req, HttpServletResponse resp) {

        String name = req.getParameter ( "userName" );
        String pwd = req.getParameter ( "passWord" );
        User user= new User (  );
        user.setUsername (name);
        user.setPassword ( pwd );
        Userservice userservice=new UserserviceImpl ();
        Boolean judgment = userservice.judgment ( user );
        req.getSession ().setAttribute ( "user",user );
        if (judgment==true){
            try {
                System.out.println ("成功");
                resp.sendRedirect ( "/jsp/admin_index.jsp" );
            }  catch (IOException e) {
                e.printStackTrace ();
            }
        }else {
            System.out.println ("账号密码错误");
        }


    }
}
